package com.emr.web.controller.emr;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emr.common.annotation.Log;
import com.emr.common.base.AjaxResult;
import com.emr.common.enums.BusinessType;
import com.emr.common.utils.ExcelUtil;
import com.emr.framework.web.page.TableDataInfo;
import com.emr.module.domain.Jieyue;
import com.emr.module.domain.Personal;
import com.emr.module.domain.Qiyongyuan;
import com.emr.module.domain.Zhikong;
import com.emr.module.service.IIcdService;
import com.emr.module.service.IJieyueService;
import com.emr.module.service.IPersonalService;
import com.emr.module.service.IQiyongyuanService;
import com.emr.module.service.IZhikongService;
import com.emr.system.domain.SysUser;
import com.emr.web.core.base.BaseController;

/**
 * 个人病例 信息操作处理
 * 
 * @author emr
 * @date 2019-02-19
 */
@Controller
@RequestMapping("/module/personal")
public class PersonalController extends BaseController
{
    private String prefix = "module/personal";
	
	@Autowired
	private IPersonalService personalService;
	@Autowired
	private IIcdService icdService;
	
	@Autowired
	private IJieyueService jieyueService;
	@Autowired
	private IQiyongyuanService qiyongyuanService;
	@Autowired
	private IZhikongService zhikongService;
	
	
	@RequiresPermissions("module:personal:view")
	@GetMapping()
	public String personal()
	{
	    return prefix + "/personal";
	}
	
	/**
	 * 查询个人病例列表
	 */
	@RequiresPermissions("module:personal:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Personal personal)
	{
		SysUser user = getUser();
		personal.setZhuzhiyisheng(user.getUserName());
		startPage();
        List<Personal> list = personalService.selectPersonalList(personal);
		return getDataTable(list);
	}
	
    @Log(title = "个人病例", businessType = BusinessType.EXPORT)
    @RequiresPermissions("module:personal:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Personal personal)
    {
        List<Personal> list = personalService.selectPersonalList(personal);
        ExcelUtil<Personal> util = new ExcelUtil<Personal>(Personal.class);
        return util.exportExcel(list, "personal");
    }
	
	
	/**
	 * 新增个人病例
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		//List<Icd> selectIcdAll = icdService.selectIcdAll();
		SysUser user = getUser();
		mmap.put("zhuzhiyisheng",user.getUserName());
		mmap.put("posts",icdService.selectIcdAll() );
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存个人病例
	 */
	@RequiresPermissions("module:personal:add")
	@Log(title = "个人病例", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Personal personal,Jieyue jieyue,Qiyongyuan qiyongyuan,Zhikong zhikong)
	{		
		SysUser user = getUser();
		if("是".equals(personal.getYuanYesOnNo())){
			qiyongyuan.setBingrenId(personal.getPatientName());
			qiyongyuan.setJibingType(personal.getYuanName());
			qiyongyuan.setJibingCode(personal.getPatientBingname());
			qiyongyuan.setQiyongyuanRen(user.getUserName());
			int insertQiyongyuan = qiyongyuanService.insertQiyongyuan(qiyongyuan);
		}
		personal.setPatientYesOnNo("否");
		personal.setZhuzhiyisheng(user.getUserName());
		int insertPersonal = personalService.insertPersonal(personal);
		jieyue.setBingrenId(personal.getId());
		jieyue.setZhuangtai(user.getUserName());
		
		jieyueService.insertJieyue(jieyue);
		zhikong.setBingrenId(personal.getPatientName());
		zhikong.setZhiliaoyishengId(personal.getZhuzhiyisheng());
		zhikong.setIcdCode(personal.getPatientBingname());
		zhikong.setPertiendId(personal.getId());
		int insertZhikong = zhikongService.insertZhikong(zhikong);
		return toAjax(insertPersonal);
	}

	/**
	 * 修改个人病例
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		//根据id查询病人id
		Personal personal = personalService.selectPersonalById(id);
		mmap.put("posts", icdService.selectIcdByIds(personal.getPatientBingname()));
		mmap.put("personal", personal);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存个人病例
	 */
	@RequiresPermissions("module:personal:edit")
	@Log(title = "个人病例", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Personal personal)
	{		
		return toAjax(personalService.updatePersonal(personal));
	}
	
	/**
	 * 删除个人病例
	 */
	@RequiresPermissions("module:personal:remove")
	@Log(title = "个人病例", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(personalService.deletePersonalByIds(ids));
	}
	
}
